package com.phkj.qdh.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.phkj.qdh.App

object SPSUtils {
    val instance: SharedPreferences by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        build()
    }

    private fun build() = App.CONTEXT.getSharedPreferences("hsh", Context.MODE_PRIVATE)

    fun put(key: String, value: Any) {
        when (value) {
            is Boolean -> {
                instance.edit { putBoolean(key, value) }
            }
            is Int -> instance.edit { putInt(key, value) }
            is String -> instance.edit { putString(key, value) }
            is Long -> instance.edit { putLong(key, value) }
            is Float -> instance.edit { putFloat(key, value) }
            else -> {}
        }
    }

    fun getString(key: String, defValue: String = ""): String {
        return instance.getString(key, "") ?: defValue
    }


    fun getBoolean(key: String, defValue: Boolean = false): Boolean {
        return instance.getBoolean(key, defValue)
    }

    fun getInt(key: String): Int {
        return instance.getInt(key, 0)
    }

    fun remove(key: String) {
        instance.edit().remove(key).apply()
    }

    fun clear() {
        instance.edit {
            this.clear()
        }
    }

}